package com.sunmi.sbs.commons.utils;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;


import java.util.HashMap;
import java.util.Properties;
import java.util.regex.*;

/**
 * author: fanjia
 * date: 2020/11/11 13:53
 * file: NacosUtils.java
 */
public class NacosUtils {
    public static String test() {
        return ("this is a test func");
    }

    public static HashMap getDevProp(String groupname, String dataid, String namespace) throws NacosException {
        Properties properties = new Properties();
        HashMap hashMap = new HashMap();
        String dataId = dataid;
        String group = groupname;
        properties.put(PropertyKeyConst.ENDPOINT, "addr-hz-internal.edas.aliyun.com");
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        properties.put(PropertyKeyConst.ACCESS_KEY, "LTAI4FjKhHWU9aZPg6UPzPYv");
        properties.put(PropertyKeyConst.SECRET_KEY, "y7vrVh1hfMK2BuhYBD1xVrsdS1o8tr");

        ConfigService configService = NacosFactory.createConfigService(properties);
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println(config);
        String dbHost = new NacosUtils().getSubUtil(config, new NacosUtils().patternBuilder("dbHost"));
        String dbPort = new NacosUtils().getSubUtil(config, new NacosUtils().patternBuilder("dbPort"));
        String dbName = new NacosUtils().getSubUtil(config, new NacosUtils().patternBuilder("dbName"));
        String dbUser = new NacosUtils().getSubUtil(config, new NacosUtils().patternBuilder("dbUser"));
        String dbPasswd = new NacosUtils().getSubUtil(config, new NacosUtils().patternBuilder("dbPasswd"));
        hashMap.put("jdbc-host", dbHost);
        hashMap.put("jdbc-port", dbPort);
        hashMap.put("jdbc-db", dbName);
        hashMap.put("jdbc-username", dbUser);
        hashMap.put("jdbc-password", dbPasswd);

        return hashMap;

    }

    public String getSubUtil(String text, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public String patternBuilder(String parttern) {
        return parttern + " = \"(.*?)\"";
    }


    public static HashMap getDbProp(String environment) throws NacosException {
        String namespace;
        HashMap hashMap;
        switch (environment) {
            case "dev":
                namespace = "713fef73-42b3-4c32-b241-96b709a3d695";
                break;
            case "test":
                namespace = "c9fcccd6-e1e1-46ae-a2e1-88a0218fa291";
                break;
            case "uat":
                namespace = "5fff5646-01dd-4ce3-9f76-02e9f0e5f1f9";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + environment);
        }

        hashMap = getDevProp("file-core", "mysql", namespace);
        return hashMap;

    }

    public static String getDbHost() throws NacosException {
        String namespace = null;
        String environment = "test";
        HashMap<String,String> hashMap;
        switch (environment) {
            case "dev":
                namespace = "713fef73-42b3-4c32-b241-96b709a3d695";
                break;
            case "test":
                namespace = "c9fcccd6-e1e1-46ae-a2e1-88a0218fa291";
                break;
            case "uat":
                namespace = "5fff5646-01dd-4ce3-9f76-02e9f0e5f1f9";
                break;
        }

        hashMap = getDevProp("file-core", "mysql", namespace);
        return hashMap.get("jdbc-host");


    }

}